{% extends 'admin_manage_Layout.html' %}

{% block content %}
    <div class="container">
        <div style="margin-bottom: 10px">

            <a id="btnAdminAdd" class="btn btn-success" href="#">
                <i class="fa fa-plus-circle" aria-hidden="true"></i>
                新建文旅
            </a>
        </div>
        <div class="bs-example" data-example-id="table-within-panel">
            <div class="panel panel-default">
                <!-- Default panel contents -->
                <div class="panel-heading">
                    <i class="fa fa-navicon" aria-hidden="true"></i>
                    文旅列表
                </div>

                <!-- Table -->
                <table class="table">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>所属地区</th>
                        <th>用户名</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for obj in querySet %}
                        <tr>
                            <td>{{ obj.id }}</td>
                            <td>{{ obj.location }}</td>
                            <td>{{ obj.username }}</td>
                            <td>
                                <a class="btn btn-primary btn-admin-edit" uid={{ obj.id }}>编辑</a>
                                <a class="btn btn-danger btn-admin-delete" uid={{ obj.id }}>删除</a>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>



    <!-- 添加/编辑 管理员模态框 -->
    <div class="modal fade" id="adminAdd-Edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel"></h4>
                </div>
                <div class="modal-body">

                    <form id="formAdd">
                        <div class="clearfix">
                            {% for field in form %}
                                <div class="col-xs-4">
                                    <div class="form-group">
                                        <label>{{ field.label }}</label>
                                        {{ field }}
                                        <span class="error-msg" style="color: red"></span>
                                    </div>
                                </div>

                            {% endfor %}
                        </div>
                    </form>


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关 闭</button>
                    <button id="btnAdminSave" type="button" class="btn btn-primary">保 存</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 删除管理员模态框 -->
    <div class="modal fade" id="adminDelete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">

            <div class="alert alert-danger alert-dismissible fade in" role="alert">

                <h4>是否确定删除？</h4>
                <p style="margin: 10px 0;">删除后所有关联数据都会被删除</p>
                <p style="text-align: right">
                    <button id="btnConfirmDelete" type="button" class="btn btn-danger">删 除</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                </p>
            </div>
        </div>
    </div>


{% endblock %}

{% block js %}

    <script type="text/javascript">

        $(function () {

            var DELETE_ID;
            var EDIT_ID;

            bindBtnAdminAddEvent();
            bindBtnAdminSaveEvent();
            bindBtnAdminDeleteEvent();
            bindBtnConfirmDeleteEvent();
            bindBtnAdminEditEvent();

        })

        function bindBtnAdminAddEvent() {
            $('#btnAdminAdd').click(function () {
                EDIT_ID = undefined;
                $("#formAdd")[0].reset();
                $("#myModalLabel").text("新建");
                $('#adminAdd-Edit').modal('show');

            });
        }

        function bindBtnAdminSaveEvent() {
            $('#btnAdminSave').click(function () {
                $(".error-msg").empty();
                if (EDIT_ID) {
                    doEdit();
                } else {
                    doAdd();
                }
            });
        }

        function doEdit() {
            //向后台发送请求
            $.ajax({
                url: "/admin/edit/" + "?uid=" + EDIT_ID,
                type: "post",
                data: $("#formAdd").serialize(),
                dataType: "JSON",
                success: function (res) {
                    if (res.status) {
                        //清空表单
                        $("#formAdd")[0].reset();
                        //关闭对话框
                        $('#adminAdd-Edit').modal('hide');
                        //刷新页面
                        location.reload()
                    } else {
                        if (res.tips) {
                            alert("res.tips")
                        } else {
                            //把错误信息显示在对话框
                            $.each(res.error, function (name, errorList) {
                                $("#id_" + name).next().text(errorList[0]);
                            })
                        }

                    }
                }
            })
        }

        function doAdd() {
            $.ajax({
                url: "/admin/add/",
                type: "POST",
                dataType: "JSON",
                data: $("#formAdd").serialize(),
                success: function (res) {
                    if (res.status) {
                        alert("添加成功！");
                        //清空表单
                        $("#formAdd")[0].reset();
                        //关闭对话框
                        $('#adminAdd-Edit').modal('hide');
                        //刷新页面
                        location.reload()
                    } else {
                        $.each(res.error, function (name, errorList) {
                            $("#id_" + name).next().text(errorList[0]);
                        })
                    }
                }
            })
        }

        function bindBtnAdminDeleteEvent() {
            $('.btn-admin-delete').click(function () {
                $('#adminDelete').modal('show');
                DELETE_ID = $(this).attr("uid")
            });
        }

        function bindBtnConfirmDeleteEvent() {
            $("#btnConfirmDelete").click(function () {
                //点击确认删除按钮，将全局变量中的设置的那个要删除的ID发送到后台
                $.ajax({
                    url: "/admin/delete/",
                    type: "GET",
                    data: {
                        uid: DELETE_ID
                    },
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            location.reload();
                        } else {
                            alert(res.error);
                        }
                    }
                })
            });
        }

        function bindBtnAdminEditEvent() {
            $(".btn-admin-edit").click(function () {
                var uid = $(this).attr("uid");
                EDIT_ID = uid;
                $.ajax({
                    url: "/admin/detail",
                    dataType: "JSON",
                    type: "get",
                    data: {
                        uid: uid
                    },
                    success: function (res) {
                        if (res.status) {
                            $.each(res.data, function (name, value) {
                                $("#id_" + name).val(value);
                            })
                            $("#myModalLabel").text("编辑");
                            $("#adminAdd-Edit").modal('show');
                        } else {
                            alert(res.error)
                        }
                    }
                })
            });
        }

    </script>

{% endblock %}